package com.nzy.minio;

import com.alibaba.fastjson.JSON;
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;

import java.util.Iterator;
import java.util.List;

public class FileUploader {
    public static void main1(String[] args) throws Exception {
        try {
            MinioClient client = MinioClient.builder().endpoint("https://play.min.io")
                    .credentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG").build();
            if(!client.bucketExists(BucketExistsArgs.builder().bucket("ningzy").build())){
                client.makeBucket(MakeBucketArgs.builder().bucket("ningzy").build());
            }
           List<Bucket> bucketList =  client.listBuckets();
            System.out.println("bucketList:" + bucketList.size());
            Iterable<Result<Item>> items = client.listObjects(ListObjectsArgs.builder().bucket("ningzy").build());
            Iterator<Result<Item>> it = items.iterator();
            while(it.hasNext()){
                Item item = it.next().get();
                System.out.println("bucket:ningzy:"
                        + item.objectName()
                        + " ,size:" + item.size());
            }
           GetObjectResponse response = client.getObject(GetObjectArgs.builder().bucket("ningzy").object("ningzy.json").build());
            if(response != null){
                System.out.println("response:" + JSON.toJSONString(response));
            }
            client.downloadObject(DownloadObjectArgs.builder().bucket("asiatrip").object("asiaphotos.zip").filename("C:\\Users\\Administrator\\Desktop\\aa.zip").build());

            /*client.uploadObject(UploadObjectArgs.builder().bucket("ningzy").object("ningzy.json")
                    .filename("C:\\Users\\Administrator\\Desktop\\view.json").build());*/
        } catch(MinioException e) {
            System.out.println("Error occurred: " + e);
        }
    }


    public static void main(String[] args) {

    }
}
